package com.c.ctgproject.adapter.guang

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapter
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.ui.collect.BatteryCanHandlerDetailActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler

class BatteryCanHandlerAdapter(private val context: Context, private val mList: List<BatteryCanHandlerEntity.DataBean.ListBean>, private val needModuleCount: Boolean = true,private val mlistener:OnCheckBoxClickedListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
//        val view = LayoutInflater.from(context).inflate(R.layout.item_batter_can_handler_list, parent, false)
        val view = LayoutInflater.from(context).inflate(R.layout.item_part_can_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mCheckBox.isChecked = data.isSelect
            holder.mNameTV.text = data.batteryName.noDataHandler()
            holder.poundNo.text = data.poundNo.noDataHandler()
            holder.mCodeTV.text = data.batterySelfCode.noDataHandler()
            holder.mPackageTV.text = data.batteryCode.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.count.text = data.count.noDataHandler()
            holder.mCarCode.text = data.carCode.noDataHandler()
            if (needModuleCount) {
                holder.moduleCount.text = data.moduleCount.noDataHandler()
                holder.moduleCountLL.visibility = View.VISIBLE
            } else {
                holder.moduleCountLL.visibility = View.GONE
            }


            val enterMode = when (data.enterMode) {
                1 -> "整车电池"
                2 -> "散件"
                3 -> "电池拆解"
                else -> "暂无数据"
            }
            holder.mFromTV.text = enterMode

            val batteryType = when (data.batteryType) {
                1 -> "电池包"
                2 -> "模组"
                3 -> "电芯"
                else -> "暂无数据"
            }
            holder.mTypeTV.text = batteryType
            //详情
            holder.mParentLL.setOnClickListener {
                val intent = Intent(context, BatteryCanHandlerDetailActivity::class.java)
                intent.putExtra(MyConstants.DATA, getGson().toJson(data))
                intent.putExtra(MyConstants.ID, data.pbId)
                context.startActivity(intent)
            }
            holder.mCheckBox.setOnClickListener {
                data.isSelect = holder.mCheckBox.isChecked
                mlistener.OnItemCheckBoxClicked()
                notifyItemChanged(position)
            }

            //是否拆解 0-未拆解 1-拆解中 2-已拆解
            holder.mCheckBox.visibility = if (data.dismantle == 0) View.VISIBLE else View.INVISIBLE


        }
    }
    interface OnCheckBoxClickedListener{
        fun OnItemCheckBoxClicked()
    }
    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val moduleCountLL: LinearLayoutCompat = view.findViewById(R.id.moduleCountll)
        val moduleCount: TextView = view.findViewById(R.id.moduleCount)
        val mCarCode: TextView = view.findViewById(R.id.mCarCode)
        val poundNo: TextView = view.findViewById(R.id.poundNo)
        val count: TextView = view.findViewById(R.id.count)
        val mNameTV: TextView = view.findViewById(R.id.mNameTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mCodeTV: TextView = view.findViewById(R.id.mCodeTV)
        val mPackageTV: TextView = view.findViewById(R.id.mPackageTV)
        val mTypeTV: TextView = view.findViewById(R.id.mTypeTV)
        val mFromTV: TextView = view.findViewById(R.id.mFromTV)
        val mCheckBox: CheckBox = view.findViewById(R.id.mCheckBox)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
    }
}